import config from './confige'


interface ComponentOptions<T extends object> {
  name?: string
  components?: Record<string, Function>
  template?: string
  setup: (props?: T) => Record<string, any>
}

function enhanceTemplate(src, mounted) {
  return src
}

function enhanceComponents(components) {
  return components
}

export default function defineComponent<T extends object>(opts: ComponentOptions<T>) {
  const components = opts.components ? enhanceComponents(opts.components) : {}

  return (props?: T) => {
    const scope = opts.setup(props)
    const $template = enhanceTemplate(opts.template || '', scope.mounted)

    if (config.__DEV__) {
      const scopeKeys = Object.keys(scope)
      const componentKeys = Object.keys(components)
      for (let i = 0; i < componentKeys.length; i++) {
        if (scopeKeys.indexOf(componentKeys[i]) !== -1) {
          console.warn(`Scope member "${componentKeys[i]}" ${ opts.name && 'of ' + opts.name } will be completely rewritten by components option.`)
        }
      }
    }

    return {
      ...scope,
      $template,
      ...components
    }
  }
}